<template>
    <div>
        <router-link tag="div" to="/" class="header-abs" v-show="showHeader">
            <div class="iconfont btn-back">&#xe624;</div>
        </router-link>
        <div
            class="header-fixed"
            v-show="!showHeader"
            :style="opacityStyle"
        >
            <router-link to="/">
                <div class="iconfont icon-back">&#xe624;</div>
            </router-link>
            {{sightName}}
        </div>
    </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showHeader: true,
      opacityStyle: {
        opacity: 0
      }
    }
  },
  props: {
    sightName: String
  },
  methods: {
    handleScroll () {
      // console.log('scroll start')
      let top = document.documentElement.scrollTop
      if (top > 60) {
        let opacity = top / 140
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = {
          opacity
        }
        this.showHeader = false
      } else {
        this.showHeader = true
      }
    }
  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll)
  },
  // 页面即将被隐藏，或者页面即将跳到新的页面执行
  unmounted () {
    // 解绑 scroll事件
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="stylus" scoped>
    @import '~styles/variables.styl'
    .header-abs
        position absolute
        z-index 2
        top .2rem
        left .2rem
        width .8rem
        height .8rem
        line-height .8rem
        border-radius .4rem
        text-align center
        background: rgba(0,0,0,0.8)
        .btn-back
            color #fff
            font-size .4rem
    .header-fixed
        position fixed
        top 0
        left 0
        width 100%
        height $headerHeight
        line-height $headerHeight
        background $bgColor
        text-align center
        color #fff
    .icon-back
        position absolute
        width $headerHeight
        font-size .4rem
        color #fff

</style>
